package com.guigu.qxl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.pojo.DFile;
import com.guigu.pojo.DModuleDetails;
import com.guigu.qxl.pojo.QDesignModule;
import com.guigu.pojo.DesignModuleDetailsVo;
import com.guigu.qxl.mapper.DesignModuleMapper;
import com.guigu.qxl.service.DesignModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DesignModuleServiceImpl implements DesignModuleService {
    @Autowired
    DesignModuleMapper designModuleMapper;

    @Override
    public boolean designModuleRegister(DesignModuleDetailsVo designModuleDetailsVo){
        String material=designModuleMapper.showMaterial();
        String[] arr1 =designModuleDetailsVo.getRegisterTime().split(" ");
        String[] arr2 =arr1[0].split("/");
        String designId="200";
        int a1=Integer.parseInt("100"+material)+1;
        String b=""+a1;
        b=b.substring(3);
        int c=designModuleMapper.updMaterial(b);
        for(String a:arr2){
            if(a.length()==1){
                a="0"+a;
            }
            designId=designId+a;
        }
        designId=designId+b;
        designModuleDetailsVo.setDesignId(designId);
        int n=designModuleMapper.register(designModuleDetailsVo);
        int id=designModuleDetailsVo.getId();
        for(DModuleDetails d:designModuleDetailsVo.getList()){
            d.setParentId(id);
            designModuleMapper.insertDetails(d);
        }
        designModuleMapper.updateModule(designModuleDetailsVo.getProductId());
        return n==1;
    }
    @Override
    public boolean designModuleChange(DesignModuleDetailsVo designModuleDetailsVo){
        int n=designModuleMapper.designModuleChange(designModuleDetailsVo);
        designModuleMapper.deleteDetailsByParentId(designModuleDetailsVo.getId());
        for(DModuleDetails d:designModuleDetailsVo.getList()){
            d.setParentId(designModuleDetailsVo.getId());
            designModuleMapper.insertDetails(d);
        }
        return n==1;
    }
    @Override
    public PageInfo<QDesignModule> pageS0010DesignModule(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<QDesignModule> list= designModuleMapper.pageS0010DesignModule();
        return new PageInfo<QDesignModule>(list);
    }
    @Override
    public PageInfo<QDesignModule> pageS0011DesignModule(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<QDesignModule> list= designModuleMapper.pageS0011DesignModule();
        return new PageInfo<QDesignModule>(list);
    }
    @Override
    public PageInfo<QDesignModule> pageDesignModule(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<QDesignModule> list= designModuleMapper.pageDesignModule();
        return new PageInfo<QDesignModule>(list);
    }
    @Override
    public QDesignModule showDesignModuleById(Integer id){
        return designModuleMapper.selectByPrimaryKey(id);
    }
    @Override
    public boolean designDesignModuleVheckOk(QDesignModule designModule){
        if(designModule.getCheckTag().equals("S001-2")){
            designModuleMapper.updateNoModule(designModule.getProductId());
        }
        System.out.println(designModule);
        return designModuleMapper.designDesignModuleVheckOk(designModule)==1;
    }
    @Override
    public List<DModuleDetails> showDesignModuleDetailsByParentId(Integer id){
        return designModuleMapper.showDesignModuleDetailsByParentId(id);
    }
}
